package com.itce.arco.util;

import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;

import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class WatermarkingEventHandler implements IEventHandler {
    @Override
    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdfDoc = docEvent.getDocument();
        PdfPage page = docEvent.getPage();
        PdfFont font = null;
        try {
            //font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
            font =PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H");//支持中文
        } catch (IOException e) {
            e.printStackTrace();
        }
        PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);

        new Canvas(pdfCanvas, page.getPageSize(), false)
                .setFontSize(60)
                .setFont(font)
                .showTextAligned(new Paragraph("我是水印\r\t鲲鹏展翅"), 298, 421, pdfDoc.getPageNumber(page),
                        TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
    }
}
